package integerFactorization;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;


public class Panels extends JPanel{
	private static final long serialVersionUID = 1L;
	final JTextField input = new JTextField(); //the number to be factored
	JTextArea CON = new JTextArea(12, 69); //the console text area
	
	/*Constructor*/
	public Panels(){
		setPreferredSize(new Dimension(800,700));
		setLayout(new BorderLayout());
		add(numberPanel(),BorderLayout.NORTH);
		add(brentPanel(),BorderLayout.EAST);
		add(polardPanel(),BorderLayout.WEST);
		add(consolPanel(),BorderLayout.SOUTH);
	}
	
	private JPanel numberPanel(){
		JPanel numberPanel = new JPanel();
		numberPanel.setPreferredSize(new Dimension(800, 75));
		numberPanel.setBackground(new Color(92,173,255));
		JLabel enterNumber = new JLabel("Enter Number: ");
		input.setPreferredSize(new Dimension(600, 30));
		numberPanel.add(enterNumber);
		numberPanel.add(input);
		return numberPanel;
	}
	
	private JPanel brentPanel(){
		Color bg = new Color(112,163,230);
		final Color gray = new Color(184,184,184);
		JPanel brentPanel = new JPanel(new BorderLayout());
		Icon image = new ImageIcon(getClass().getResource("img/brent.png"));
		JLabel brent = new JLabel(image);
		JButton Go = new JButton("GO");
		JPanel btnPanel = new JPanel(new GridLayout(1, 2));
		JPanel firstLinePanel = new JPanel(new BorderLayout());
		JPanel inputPanel = new JPanel(new GridLayout(4, 1));
		final JCheckBox cbox = new JCheckBox("Random Input");
		JPanel secondLinePanel = new JPanel();
		JLabel polynomial = new JLabel("f(X) = X^2 + ");
		final JTextField c_brent = new JTextField();
		JPanel thirdLinePanel = new JPanel();
		JLabel initialValue = new JLabel("X0 = ");
		final JTextField x0_brent = new JTextField();
		JPanel fourthLinePanel = new JPanel();
		JLabel mLabel = new JLabel("m = ");
		final JTextField m = new JTextField();
		brentPanel.setPreferredSize(new Dimension(400, 400));
		brentPanel.setBackground(bg);
		Go.setBackground(bg);
		Go.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(cbox.isSelected()){
					try{
						BigInteger num = new BigInteger(input.getText());
						long start = System.nanoTime();
						Calculation.brentFactorRandom(num,CON);
						long end = System.nanoTime();
						CON.append("and total Time is "+(end-start)/(1000)+" microseconds.\n");
					}catch (Exception e) {
						JOptionPane.showMessageDialog(null, "Input mismatch!");
					}
				}
				else{
					try{
						BigInteger num = new BigInteger(input.getText());
						BigInteger constant = new BigInteger(c_brent.getText());
						BigInteger initialValue = new BigInteger(x0_brent.getText());
						BigInteger m_input = new BigInteger(m.getText());
						long start = System.nanoTime();
						Calculation.brentFactorPolynomial(num, constant, initialValue, m_input,CON);
						long end = System.nanoTime();
						CON.append("and total Time is "+(end-start)/(1000)+" microseconds.\n");
					}catch (Exception e) {
						JOptionPane.showMessageDialog(null, "Input mismatch!");
					}
				}
			}
		});
		btnPanel.add(Go);
		inputPanel.setBackground(bg);
		firstLinePanel.setBackground(bg);
		cbox.setBackground(bg);
		cbox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(cbox.isSelected()){
					c_brent.setText("");
					x0_brent.setText("");
					m.setText("");
					c_brent.setEditable(false);
					x0_brent.setEditable(false);
					m.setEditable(false);
					c_brent.setBackground(gray);
					x0_brent.setBackground(gray);
					m.setBackground(gray);
				}
				if(!(cbox.isSelected())){
					c_brent.setEditable(true);
					x0_brent.setEditable(true);
					m.setEditable(true);
					c_brent.setBackground(Color.white);
					x0_brent.setBackground(Color.white);
					m.setBackground(Color.white);
				}
			}
		});
		firstLinePanel.add(cbox,BorderLayout.WEST);
		inputPanel.add(firstLinePanel);
		secondLinePanel.setBackground(bg);
		c_brent.setPreferredSize(new Dimension(70, 25));
		secondLinePanel.add(polynomial);
		secondLinePanel.add(c_brent);
		inputPanel.add(secondLinePanel);
		thirdLinePanel.setBackground(bg);
		x0_brent.setPreferredSize(new Dimension(70,25));
		thirdLinePanel.add(initialValue);
		thirdLinePanel.add(x0_brent);
		inputPanel.add(thirdLinePanel);
		fourthLinePanel.setBackground(bg);
		m.setPreferredSize(new Dimension(70,25));
		fourthLinePanel.add(mLabel);
		fourthLinePanel.add(m);
		inputPanel.add(fourthLinePanel);
		brentPanel.add(inputPanel,BorderLayout.CENTER);
		brentPanel.add(brent,BorderLayout.BEFORE_FIRST_LINE);
		brentPanel.add(btnPanel,BorderLayout.SOUTH);
		return brentPanel;
	}
	
	private JPanel polardPanel(){
		Color bg = new Color(102,153,230);
		final Color gray = new Color(184,184,184);
		JPanel polardPanel = new JPanel(new BorderLayout());
		Icon image = new ImageIcon(getClass().getResource("img/pollard.png"));
		JLabel pollard = new JLabel(image);
		JButton Go = new JButton("GO");
		JPanel btnPanel = new JPanel(new GridLayout(1, 2));
		JPanel inputPanel = new JPanel(new GridLayout(3, 1));
		JPanel firstLinePanel = new JPanel(new BorderLayout());
		final JCheckBox cbox = new JCheckBox("Random Input");
		JPanel secondLinePanel = new JPanel();
		JLabel polynomial = new JLabel("f(X) = X^2 + ");
		final JTextField c_polard = new JTextField();
		JPanel thirdLinePanel = new JPanel();
		JLabel initialValue = new JLabel("X0 = ");
		final JTextField x0_polard = new JTextField();
		polardPanel.setPreferredSize(new Dimension(400, 400));
		polardPanel.setBackground(bg);
		Go.setBackground(bg);
		Go.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(cbox.isSelected()){
					try{
					BigInteger num = new BigInteger(input.getText());
					long start = System.nanoTime();
					Calculation.polardFactorRandom(num, CON);
					long end = System.nanoTime();
					CON.append("and total Time is "+(end-start)/(1000)+" microseconds.\n");
					}catch (Exception e) {
						JOptionPane.showMessageDialog(null, "Input mismatch!");
					}
				}
				else{
					try{
						BigInteger num = new BigInteger(input.getText());
						BigInteger constant = new BigInteger(c_polard.getText());
						BigInteger initialValue = new BigInteger(x0_polard.getText());
						long start = System.nanoTime();
						Calculation.polardFactorPolynomial(num, constant, initialValue, CON);
						long end = System.nanoTime();
						CON.append("and total Time is "+(end-start)/(1000)+" microseconds.\n");
					}catch (Exception e) {
						JOptionPane.showMessageDialog(null, "Input mismatch!");
					}
				}
			}
		});
		btnPanel.add(Go);
		inputPanel.setBackground(bg);
		firstLinePanel.setBackground(bg);
		cbox.setBackground(bg);
		cbox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(cbox.isSelected()){
					c_polard.setText("");
					x0_polard.setText("");
					c_polard.setEditable(false);
					x0_polard.setEditable(false);
					c_polard.setBackground(gray);
					x0_polard.setBackground(gray);
				}
				if(!(cbox.isSelected())){
					c_polard.setEditable(true);
					x0_polard.setEditable(true);
					c_polard.setBackground(Color.white);
					x0_polard.setBackground(Color.white);
				}
			}
		});
		firstLinePanel.add(cbox,BorderLayout.WEST);
		inputPanel.add(firstLinePanel);
		secondLinePanel.setBackground(bg);
		c_polard.setPreferredSize(new Dimension(70, 25));
		secondLinePanel.add(polynomial);
		secondLinePanel.add(c_polard);
		inputPanel.add(secondLinePanel);
		thirdLinePanel.setBackground(bg);
		x0_polard.setPreferredSize(new Dimension(70,25));
		thirdLinePanel.add(initialValue);
		thirdLinePanel.add(x0_polard);
		inputPanel.add(thirdLinePanel);
		polardPanel.add(inputPanel,BorderLayout.CENTER);
		polardPanel.add(pollard,BorderLayout.BEFORE_FIRST_LINE);
		polardPanel.add(btnPanel,BorderLayout.SOUTH);
		return polardPanel;
	}
	
	private JPanel consolPanel(){
		Color bg = new Color(153,204,255);
		JPanel consolPanel = new JPanel();
		consolPanel.setPreferredSize(new Dimension(800,225));
		consolPanel.setBackground(bg);
		consolPanel.setBorder(new TitledBorder(new EtchedBorder(),"Console"));
		CON.setBackground(bg);
		CON.setEditable(false);
		JScrollPane scroll = new JScrollPane(CON);
		scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		consolPanel.add(scroll);
		return consolPanel;
	}
}
